<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use think\App;

class Carousel extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Carousel';

    //验证器
    protected string $validate = 'Carousel';

    protected array $append = ['position_name', 'type_name'];

    public function index(): string
    {
        return Builder::Table()
            ->addColumn(['fixed' => 'left', 'title' => '全选', 'type' => 'checkbox'])
            ->addColumn(['field' => 'id', 'title' => 'ID', 'sort' => true, 'width' => 80])
            ->addColumn(['field' => 'title', 'title' => '标题', 'modify' => '2,input'])
            ->addColumn(['field' => 'position_name', 'title' => '位置'])
            ->addColumn(['field' => 'position', 'title' => '位置', 'modify' => '2,select,getPosition', 'hide' => true])
            ->addColumn(['field' => 'type_name', 'title' => '类型'])
            ->addColumn(['field' => 'type', 'title' => '类型', 'modify' => '2,select,getType', 'unite' => 'getTypeUnite', 'hide' => true])
            ->addColumn(['field' => 'images', 'title' => '图片', 'modify' => '2,img', 'templet' => '#imagesTpl'])
            ->addColumn(['field' => 'video', 'title' => '视频', 'modify' => '2,video', 'templet' => '#videoTpl', 'hide' => true])
            ->addColumn(['field' => 'link', 'title' => '链接', 'modify' => '2,input'])
            ->addColumn(['field' => 'status', 'title' => '状态', 'templet' => '#status', 'width' => 120])
            ->addColumn(['field' => 'sort', 'title' => '排序', 'modify' => '2,number', 'templet' => '#sort_sort', 'width' => 120])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['fixed' => 'right', 'title' => '操作', 'align' => 'center', 'toolbar' => '#barDemo', 'width' => 150])
            ->addSwitchTool([['name' => 'status', 'pk' => $this->pk, 'text' => '启用|停用', 'val' => '1|0', 'checked' => 1]])
            ->addSortTool([['name' => 'sort', 'pk' => $this->pk, 'url' => url('update')->build()]])
            ->addRightButtons(['edit' => ['size' => '50%'], 'delete'])
            ->addTopButtons(['add' => ['size' => '50%'], 'deleteAll'])
            ->addSearch([
                ['title', '标题', 'input,Like']
            ])
            ->fetch();
    }

    public static function getPosition(): array
    {
        return getPosition();
    }

    public static function getType(): array
    {
        return [1 => '图片'];
    }

    public static function getTypeUnite(): array
    {
        return [1 => ['images'], 2 => ['video']];
    }
}